using System;
using System.Data;
using HRM.Common;
namespace HRM.BusinessLogic
{
    public class ContractTypeInfo : Standard_Info
    {

        public ContractTypeInfo()
        {
        }


        private Int32 _ContractType_Id;
        public Int32 ContractType_Id
        {
            get { return _ContractType_Id; }
            set { _ContractType_Id = value; }
        }

        private String _ContractType_Code;
        public String ContractType_Code
        {
            get { return _ContractType_Code; }
            set { _ContractType_Code = value; }
        }

        private String _ContractType_Name;
        public String ContractType_Name
        {
            get { return _ContractType_Name; }
            set { _ContractType_Name = value; }
        }

        private Decimal _ContractType_ProbationSalary;
        public Decimal ContractType_ProbationSalary
        {
            get { return _ContractType_ProbationSalary; }
            set { _ContractType_ProbationSalary = value; }
        }

        private int _ContractType_Time;
        public int ContractType_Time
        {
            get { return _ContractType_Time; }
            set { _ContractType_Time = value; }
        }
        private Boolean _ContractType_Insurance;
        public Boolean ContractType_Insurance
        {
            get { return _ContractType_Insurance; }
            set { _ContractType_Insurance = value; }
        }

        private String _ContractType_BranchCode;
        public String ContractType_BranchCode
        {
            get { return _ContractType_BranchCode; }
            set { _ContractType_BranchCode = value; }
        }
    }

    public class ContractType_Controller : Standard_Controller
    {
        #region "Contructor"
        private static ContractType_Controller _instance;
        protected ContractType_Controller() { }
        protected new void Dispose()
        {
            _instance = null;
        }
        private static object _syncLock = new object();
        public static ContractType_Controller Instance()
        {
            if (_instance == null)
            {
                lock (_syncLock)
                {
                    if (_instance == null)
                        _instance = new ContractType_Controller();
                }
            }
            return _instance;
        }
        public static ContractType_Controller Instance(String transName)
        {
            if (!String.IsNullOrEmpty(transName))
            {
                ContractType_Controller _instanceTrans = new ContractType_Controller();
                _instanceTrans.Trans_Name = transName;
                return _instanceTrans;
            }
            else
                return Instance();
        }
        #endregion

        public const String cs_Table_Name = "HRM_ContractType";

        public int ContractType_Using(int ContractTypeID)
        {
            return Globals.Object_GetValue<int>(DataConnect.ExecuteScalar(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_ContractType_Using",false), ContractTypeID));
        }

        public DataSet ContractType_GetAll()
        {
            return DataConnect.ExecuteDataset(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_ContractType_GetAll", false));
        }

        public DataSet ContractType_GetList(out int ItemCount, string branchcode, int currentPage, int pageSize)
        {
            ItemCount = 0;
            DataSet ds = DataConnect.ExecuteDataset(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_ContractType_GetList", false), currentPage, pageSize,branchcode);
            if (ds != null && ds.Tables.Count > 0)
                ItemCount = Globals.Object_GetValue<int>(ds.Tables[1].Rows[0][0]);
            return ds;
        }

        public ContractTypeInfo ContractType_GetInfo(int ContractTypeID)
        {
            return CBO.FillObject<ContractTypeInfo>(DataConnect.ExecuteRow(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_ContractType_GetInfo", false), ContractTypeID));
        }

        public int ContractType_Insert(ContractTypeInfo eInfo)
        {
            //No care
            eInfo.ContractType_Insurance = false;
            eInfo.ContractType_ProbationSalary = 0;
            //
            return Globals.Object_GetValue<int>(DataConnect.ExecuteScalar(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_ContractType_Insert", false),
                eInfo.ContractType_Name, 
                eInfo.ContractType_Code,
                eInfo.ContractType_ProbationSalary,
                eInfo.ContractType_Time,
                eInfo.ContractType_Insurance,
                eInfo.ContractType_BranchCode));
        }

        public bool ContractType_Update(ContractTypeInfo eInfo)
        {
            //No care
            eInfo.ContractType_Insurance = false;
            eInfo.ContractType_ProbationSalary = 0;
            //
            return Globals.Object_GetValue<bool>(DataConnect.ExecuteScalar(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_ContractType_Update", false), 
                eInfo.ContractType_Id,
                eInfo.ContractType_Name,
                eInfo.ContractType_Code,
                eInfo.ContractType_ProbationSalary,
                eInfo.ContractType_Time,
                eInfo.ContractType_Insurance,
                eInfo.ContractType_BranchCode));
        }

        public bool ContractType_Delete(int ContractTypeID)
        {
            return Globals.Object_GetValue<bool>(DataConnect.ExecuteScalar(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_ContractType_Delete", false), ContractTypeID));
        }


        public void InsertTable(DataTable dtData)
        {
            using (var dt = dtData.GetChanges(DataRowState.Deleted))
            {
                if(dt!=null)
                {
                    var dataTable = dt.GetChanges(DataRowState.Deleted);
                    if (dataTable != null)
                        foreach (DataRow dr in dataTable.Rows)
                        {
                            ContractType_Delete(Globals.DB_GetValue<int>(dr["ContractType_ID", DataRowVersion.Original]));
                        }
                }
            }
            using (var dt = dtData.GetChanges(DataRowState.Added))
            {
                if (dt != null)
                {
                    var dataTable = dt.GetChanges(DataRowState.Added);
                    if (dataTable != null)
                        foreach (DataRow dr in dataTable.Rows)
                        {
                            var objInfo = new ContractTypeInfo();
                            Globals.DataRow_FillObject(dr, objInfo);
                            ContractType_Insert(objInfo);
                        }
                }
            }
            using (var dt = dtData.GetChanges(DataRowState.Modified))
            {
                if (dt != null)
                {
                    var dataTable = dt.GetChanges(DataRowState.Modified);
                    if (dataTable != null)
                        foreach (DataRow dr in dataTable.Rows)
                        {
                            var objInfo = new ContractTypeInfo();
                            Globals.DataRow_FillObject(dr, objInfo);
                            ContractType_Update(objInfo);
                        }
                }
            }

            //
            dtData.AcceptChanges();
            //
           
        }

    }
}
